﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UniTAC.Properties;
using Microsoft.Win32;
using System.IO;
// using Hotkeys;

namespace UniTAC
{
    public partial class StartWindow : Form
    {
        public StartWindow()
        {
            InitializeComponent();
            notifyIcon1.Visible = false;
            Width = 314;
            ghk = new Hotkeys.GlobalHotkey(0x0001 + 0x0002, Keys.U, this); // Alt+Ctrl+U
            if (Settings.Default.AutoSuggest == true)
                checkBox1.Checked = true;
            else checkBox1.Checked = false;
            if (Settings.Default.MultipleOccurrence == true)
                checkBox2.Checked = true;
            else checkBox2.Checked = false;
            if (Settings.Default.Startup == true)
                checkBox3.Checked = true;
            else checkBox3.Checked = false;
            textBox2.Text = Settings.Default.LetterLimit.ToString();
            this.ActiveControl = button1;
        }

        /* ------- Setup Hotkey ------- */
        private Hotkeys.GlobalHotkey ghk;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312)
            {
                // Here goes the code that is executed when hotkey is pressed!
                if (Settings.Default.AutoSuggest == true)
                {
                    Settings.Default.AutoSuggest = false;
                    notifyIcon1.BalloonTipText = "UniTAC Suggestions is turned off.";
                    notifyIcon1.BalloonTipTitle = "UniTAC Notification";
                    notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                    notifyIcon1.ShowBalloonTip(150);
                }
                else
                {
                    Settings.Default.AutoSuggest = true;
                    notifyIcon1.BalloonTipText = "UniTAC Suggestions is turned on.";
                    notifyIcon1.BalloonTipTitle = "UniTAC Notification";
                    notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                    notifyIcon1.ShowBalloonTip(150);
                }
            }
            base.WndProc(ref m);
        }
        /* ---------------------------- */

        AutoCompleteMenu acMenu = new AutoCompleteMenu();
        private void button1_Click(object sender, EventArgs e)
        {
            notifyIcon1.Visible = true;
            notifyIcon1.BalloonTipText = "Don't worry, We are here..";
            notifyIcon1.BalloonTipTitle = "UniTAC Notification";
            notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
            notifyIcon1.ShowBalloonTip(500);

            if (checkBox1.Checked == true)
                Settings.Default.AutoSuggest = true;
            else Settings.Default.AutoSuggest = false;

            if (checkBox2.Checked == true)
                Settings.Default.MultipleOccurrence = true;
            else Settings.Default.MultipleOccurrence = false;

            RegistryKey key = Registry.CurrentUser.OpenSubKey(
                @"Software\Microsoft\Windows\CurrentVersion\Run", true);
            if (checkBox3.Checked == true)
            {
                key.SetValue("UniTAC", "\"" + Application.ExecutablePath + "\"");
                Settings.Default.Startup = true;
            }
            else
            {
                try
                {
                    key.DeleteValue("UniTAC");
                }
                catch (Exception)
                { }
                Settings.Default.Startup = false;
            }

            if (textBox2.Text.Length > 0)
                Settings.Default.LetterLimit = Int32.Parse(textBox2.Text);
            this.Hide();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Settings.Default.Save();
            Application.Exit();
        }

        private void Go_Up(object sender, EventArgs e)
        {
            this.Show();
            notifyIcon1.Visible = false;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.Show();
            notifyIcon1.Visible = false;
        }

        private void b4_close(object sender, FormClosingEventArgs e)
        {
            if (this.Visible == true)
            {
                e.Cancel = true;
                notifyIcon1.Visible = true;
                notifyIcon1.BalloonTipText = "Don't worry, We are here..";
                notifyIcon1.BalloonTipTitle = "UniTAC Notification";
                notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                notifyIcon1.ShowBalloonTip(300);
                this.Hide();
            }
            else ghk.Unregister();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (panel1.Visible == false)
            {
                Width = 598;
                panel1.Visible = true;
                button2.Text = "<<";
            }
            else
            {
                Width = 314;
                panel1.Visible = false;
                button2.Text = ">>";
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) || e.KeyChar == '0')
            {
                e.Handled = true;
            }
        }

        private void StartWindow_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true)
            {
                if (Settings.Default.AutoSuggest == true)
                    checkBox1.Checked = true;
                else checkBox1.Checked = false;
                if (Settings.Default.MultipleOccurrence == true)
                    checkBox2.Checked = true;
                else checkBox2.Checked = false;
                if (Settings.Default.Startup == true)
                    checkBox3.Checked = true;
                else checkBox3.Checked = false;
                textBox2.Text = Settings.Default.LetterLimit.ToString();
                
                ghk.Unregister();
            }
            else
            {
                ghk.Register();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                checkBox2.Enabled = true;
            }
            else
            {
                checkBox2.Enabled = false;
                checkBox2.Checked = false;
            }
        }

        private void StartWindow_Load(object sender, EventArgs e)
        {
            if (Settings.Default.Startup == true)
            {
                this.Visible = false;
                notifyIcon1.Visible = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int addCount = 0;
            string trimmedWord;
            string[] allWords;
            if (string.IsNullOrEmpty(textBox1.Text))
                MessageBox.Show("Please enter at least one word!");
            else
            {
                string[] newWords = textBox1.Text.Split('\n');
                foreach (string eachWord in newWords)
                {
                    if (((trimmedWord = eachWord.Trim()).Length > 1) && (trimmedWord.All(Char.IsLetter)))
                    {
                        //MessageBox.Show(trimmedWord);

                        if (char.ToUpper(trimmedWord[0]) == 'A')
                        {
                            allWords = File.ReadAllLines("Words/A.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/A.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }

                        else if (char.ToUpper(trimmedWord[0]) == 'B')
                        {
                            allWords = File.ReadAllLines("Words/B.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/B.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'C')
                        {
                            allWords = File.ReadAllLines("Words/C.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/C.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'D')
                        {
                            allWords = File.ReadAllLines("Words/D.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/D.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'E')
                        {
                            allWords = File.ReadAllLines("Words/E.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/E.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'F')
                        {
                            allWords = File.ReadAllLines("Words/F.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/F.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'G' || char.ToUpper(trimmedWord[0]) == 'H')
                        {
                            allWords = File.ReadAllLines("Words/GH.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/GH.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'I')
                        {
                            allWords = File.ReadAllLines("Words/I.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/I.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'J' || char.ToUpper(trimmedWord[0]) == 'K' || char.ToUpper(trimmedWord[0]) == 'L')
                        {
                            allWords = File.ReadAllLines("Words/JKL.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/JKL.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'M')
                        {
                            allWords = File.ReadAllLines("Words/M.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/M.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'N' || char.ToUpper(trimmedWord[0]) == 'O')
                        {
                            allWords = File.ReadAllLines("Words/NO.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/NO.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'P')
                        {
                            allWords = File.ReadAllLines("Words/P.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/P.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'Q' || char.ToUpper(trimmedWord[0]) == 'R')
                        {
                            allWords = File.ReadAllLines("Words/QR.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/QR.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'S')
                        {
                            allWords = File.ReadAllLines("Words/S.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/S.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'T')
                        {
                            allWords = File.ReadAllLines("Words/T.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/T.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'U' || char.ToUpper(trimmedWord[0]) == 'V')
                        {
                            allWords = File.ReadAllLines("Words/UV.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/UV.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                        else if (char.ToUpper(trimmedWord[0]) == 'W' || char.ToUpper(trimmedWord[0]) == 'X' || char.ToUpper(trimmedWord[0]) == 'Y' || char.ToUpper(trimmedWord[0]) == 'Z')
                        {
                            allWords = File.ReadAllLines("Words/WXYZ.txt");
                            if (!allWords.Contains(trimmedWord, StringComparer.OrdinalIgnoreCase))
                            {
                                File.AppendAllText("Words/WXYZ.txt", Environment.NewLine + trimmedWord);
                                addCount++;
                            }
                        }
                    }
                }
                MessageBox.Show(addCount + " new word(s) added!");
            }

        }
    }
}
